﻿using BsForApi.Model;
using BsForApi.Model.Account;
using BsForApi.Tool;
using HCDV.HCSP;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BsForApi
{
    /// <summary>
    /// 任务采收加工发货相关
    /// </summary>
    public class TaskGJS
    {

        /// <summary>
        /// 获取进行中的作业
        /// </summary>
        /// <param name="loginKye"></param>
        /// <returns></returns>
        public static ApiOut<List<TaskInfo>> GetTaskDoingList(string loginKey, string userId)
        {
            ApiOut<List<TaskInfo>> re = new ApiOut<List<TaskInfo>>();
            ApiOut<LoginOut> user = Account.getUserInfoByKey(loginKey, userId);
            if (!user.State)
            {
                re.SetError(user.Desc);
                return re;
            }
            using (dbsimple db = new dbsimple())
            {
                var dt = db.Query(string.Format(@"SELECT id,taskno,tasksctype,tasksctypename,taskmj,date_format(taskstarttime,'%Y-%c-%d') as  taskstarttime,maindcid,maindcinfo
 from hsc_task where  databranchid={0} and (taskstatus ='种植' or taskstatus='计划');", user.Info.UserOrgId));
                re.Info = (List<TaskInfo>)ModelConvertHelper<TaskInfo>.ConvertToModel(dt);
            }
            return re;
        }



        /// <summary>
        /// 登录处理
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public static ApiOut<LoginOut> Login(Loginin userinfo)
        {
            ApiOut<LoginOut> re = new ApiOut<LoginOut>();
            DataTable user;
            Hashtable allinfo = new Hashtable();
            allinfo.Add("userid", userinfo.UserAccount);
            using (dbsimple db = new dbsimple())
            {
                user = db.getProc("getuserbylid", allinfo).Tables[0];
            }
            if (user == null || user.Rows.Count == 0)
            {
                re.SetError("账号不存在");
            }
            else
            {
                DataRow row = user.Rows[0];
                if (row["password"].ToString().Equals(userinfo.UserPassWord))
                {
                    LoginOut lu = new LoginOut();
                    lu.UserId = row["empid"].ToString();
                    // lu.OrgCode = row["SimpleCode"].ToString();
                    lu.UserName = userinfo.UserAccount;
                    lu.UserDepId = row["DepId"].ToString();
                    lu.UserOrgId = row["branchId"].ToString();
                    lu.BranchType = row["branchType"].ToString();
                    lu.UserTitle = row["roles"].ToString();
                    lu.UserDepName = row["depname"].ToString();
                    lu.UserOrgName = row["branchname"].ToString();
                    lu.LoginKey = Guid.NewGuid().ToString();
                    using (dbsimple db = new dbsimple())
                    {
                        db.SpExe(string.Format("update mrbaseinf set  loginTime='{1}',loginkey='{2}',logininfo='{3}' where EmpID={0}",
                            lu.UserId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), lu.LoginKey, JsonConvert.SerializeObject(lu)
                            ));
                    }
                    re.Info = lu;

                }
                else
                {
                    re.SetError("密码不正确");
                }
            }
            return re;


        }
    }
}
